//
//  BaseViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "BaseViewController.h"
#import "UIButton+Util.h"

#import "YouTuiSDK.h"
#import "FreeGeekSheet.h"

@interface BaseViewController ()
{
    YouTuiSDK * YTsdk;
    NSArray *titleArray;
    NSArray *imageArray;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = NavigatorBackgroundColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    __weak typeof (self) weakSelf = self;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)weakSelf.navigationController;
    }
    
    YTsdk = [[YouTuiSDK alloc] init];
    titleArray =
    @[@"微信",@"微信朋友圈",@"新浪微博",@"QQ",@"QQ空间",@"复制链接"];
    imageArray = @[@"wechat",@"wechatf",@"sina",@"qq",@"qqzone",@"mark"];
}

- (void)setupCustomBackItem {
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_top_icon3"] style:UIBarButtonItemStyleDone target:self action:@selector(popViewController)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)popViewController {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)setupValidateLeftItem:(BOOL)isAuthen {
    
    UIButton *authenB = [UIButton buttonWithType:UIButtonTypeCustom];
    authenB.frame = CGRectMake(0, 0, 60, 44);
    [authenB setImage:[UIImage imageNamed:@"nav_top_icon1"] forState:0];
    [authenB setImage:[UIImage imageNamed:@"nav_top_icon1"] forState:UIControlStateHighlighted];
    [authenB setTitle:isAuthen ? @"已认证" : @"未认证" forState:0];
    [authenB setTitleColor:[UIColor whiteColor] forState:0];
    [authenB centerImageAndTitle:0];
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithCustomView:authenB];
    self.navigationItem.leftBarButtonItem = shareItem;
}

- (void)setupShareRightItem {
    
    UIButton *shareB = [UIButton buttonWithType:UIButtonTypeCustom];
    shareB.frame = CGRectMake(0, 0, 44, 44);
    [shareB setImage:[UIImage imageNamed:@"nav_top_icon2"] forState:0];
    [shareB setTitle:@"分享" forState:0];
    [shareB setTitleColor:[UIColor whiteColor] forState:0];
    [shareB centerImageAndTitle:0];
    [shareB addTarget:self action:@selector(button_share) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithCustomView:shareB];
    self.navigationItem.rightBarButtonItem = shareItem;
}

- (void)button_share {
    
    FreeGeekSheet * Sheet = [[FreeGeekSheet alloc]initWithTitle:@"分享到"
                                                       Delegate:(id<FreeGeekDelegate>)self
                                                     titleArray:titleArray
                                                     imageArray:imageArray
                                                     PointArray:nil
                                                     ShowRedDot:NO
                                                   ActivityName:nil
                                                         Middle:NO];
    [Sheet ShowInView:self.view];
}

-(void)ShareButtonAction:(NSInteger *)buttonIndex {
    
#if SHARE_NEED_TID == 1
    NSString *shareText = [NSString stringWithFormat:SHARE_URL, [[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]];
#else
    NSString *shareText = SHARE_URL;
#endif
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy-MM-dd hh:mm";
    
    switch ((int)buttonIndex) {
        case 0:
//            [YTsdk WxShareMessage:shareText
//                             Type:0];
            [YTsdk WxShareTitle:SHARE_TITLE Description:SHARE Image:[UIImage imageNamed:@"app_icon_60"] Url:shareText publicTime:[df stringFromDate:[NSDate date]] ArtID:nil Type:0];
            break;
        case 1:
//            [YTsdk WxShareMessage:shareText
//                             Type:1];
            [YTsdk WxShareTitle:SHARE_TITLE Description:SHARE Image:[UIImage imageNamed:@"app_icon_60"] Url:shareText publicTime:[df stringFromDate:[NSDate date]] ArtID:nil Type:1];
            break;
        case 2: {
            [YouTuiSDK SinaWbShareMessage:SHARE
                                    Image:[UIImage imageNamed:@"app_icon_60"]
                                      Url:shareText
                              RedirectURI:nil
                              AccessToken:nil
                               publicTime:[df stringFromDate:[NSDate date]]
                                 ArtTitle:SHARE_TITLE
                                    ArtID:@"SINA3"];
        }
            break;
        case 3:
//            [YTsdk QQShareTextContent:shareText];
            [YTsdk QQShareNewsTitle:SHARE_TITLE Description:SHARE Image:[UIImage imageNamed:@"app_icon_60"] Url:shareText publicTime:[df stringFromDate:[NSDate date]] ArtID:nil Type:0];
            break;
        case 4:
//            [YTsdk QQShareTextContent:shareText];
            [YTsdk QQShareNewsTitle:SHARE_TITLE Description:SHARE Image:[UIImage imageNamed:@"app_icon_60"] Url:shareText publicTime:[df stringFromDate:[NSDate date]] ArtID:nil Type:1];
            break;
//        case 5:
//            [YTsdk checkAuthValidDelegate:self];
//            [YTsdk TcWbLoginWithDelegate:self andRootController:self];
//            break;
        default:
            [YouTuiSDK CopyLinkWithLink:shareText];
            break;
    }
}

-(void)didCheckAuthValid:(BOOL)bResult suggest:(NSString *)strSuggestion
{
    if(bResult) //授权有效
    {
#if SHOW_WEIZHUAN == 1
        NSString *shareText = [NSString stringWithFormat:SHARE_URL, CLIENT_ID,[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]];
#else
        NSString *shareText = [NSString stringWithFormat:SHARE_URL, CUST_ID];
#endif
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"yyyy-MM-dd hh:mm";
        
        [YTsdk TcWbShareMessage:SHARE
                           Link:shareText
                    andImageUrl:@"http://img1.imgtn.bdimg.com/it/u=524667967,2954926626&fm=21&gp=0.jpg"
                       VideoUrl:nil
                      Longitude:nil
                       Latitude:nil
                       MusicUrl:nil
                     MusicTitle:nil
                    MusicAuthor:nil
                     publicTime:[df stringFromDate:[NSDate date]]
                          ArtID:@"TCWB1"
                       delegate:self];
    }
    else
    {
        [YTsdk TcWbLoginWithDelegate:self andRootController:self];
    }
}

-(void)DidAuthFinished:(WeiboApiObject *)wbobj
{
    [YTsdk TcWbRequestUserInfoDelegate:self];   //登陆成功获取当前授权用户的信息
    
}

-(void)DidAuthCanceled:(WeiboApiObject *)wbobj
{
    
}

-(void)DidAuthFailWithError:(NSError *)error
{
    [YTsdk TcWbLoginWithDelegate:self andRootController:self];
}

-(void)didNeedRelogin:(NSError *)error reqNo:(int)reqno{
    
    [YTsdk TcWbLoginWithDelegate:self andRootController:self];
}

@end
